﻿Shader "FLL/chapter8/透明度混合"
{
    Properties
    {
        _Color("[漫反射颜色] color",Color)=(1,1,1,1)
        _MainTex ("[主纹理] texture", 2D) = "white" {}
        _AlphaScale("[整体透明度] alphascale",Range(0,1))=1
    }
    SubShader
    {
        Tags { "Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="Transparent"}

        //------------------------[第一个pass]----------------------------------
        Pass
        {
            Tags{"LightMode"="ForwardBase"}
            Cull Front
            // 关闭深度写入,这个不能放在cg里面
            // ZWrite Off
            // 设置 Pass 的混合模式,定义材质参数
            // Blend | Srcfactor | Dstfactor
            Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
            #include "Lighting.cginc"


            sampler2D _MainTex;
            float4 _MainTex_ST;
            float4 _Color;
            fixed _AlphaScale;

            struct appdata
            {
                float4 vertex : POSITION;
                float3 normal:NORMAL;
                float4 texcoord:TEXCOORD0;
            };

            struct v2f
            {
                float4 pos:SV_POSITION;
                float3 worldNormal:TEXCOORD0;
                float3 worldPos:TEXCOORD1;
                float2 uv:TEXCOORD2;

            };



            v2f vert (appdata v)
            {
                
                v2f o;
                o.pos=UnityObjectToClipPos(v.vertex);
                o.worldNormal=UnityObjectToWorldNormal(v.normal);
                o.worldPos=mul(unity_ObjectToWorld,v.vertex).xyz;
                o.uv=TRANSFORM_TEX(v.texcoord,_MainTex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed3 worldNormal=normalize(i.worldNormal);
                fixed3 worldLightDir=normalize(UnityWorldSpaceLightDir(i.worldPos));

                fixed4 texColor=tex2D(_MainTex,i.uv);

                //----------------------[透明度混合 (alpha blend)]-----------------------
                /*
                *   啥也不用写,只是把透明测试的代码移除了就好
                *   我们前面已经用Blend命令打开了混合后,后面设置该片元的透明通道才有意义
                */


                
                fixed3 albedo=texColor.rgb*_Color.rgb;
                fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz*albedo;
                fixed3 diffuse=_LightColor0.rgb*albedo*max(0,dot(worldNormal,worldLightDir));
                //这里的alpha通道不再是1了,我们自己规定了相应的东西
                return fixed4(ambient+diffuse,texColor.a*_AlphaScale);


            }
            ENDCG
        }

        //------------------------[第二个pass]----------------------------------
        Pass
        {
            Tags{"LightMode"="ForwardBase"}
            Cull Back
            // 关闭深度写入,这个不能放在cg里面
            // ZWrite Off
            // 设置 Pass 的混合模式,定义材质参数
            // Blend | Srcfactor | Dstfactor
            Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
            #include "Lighting.cginc"


            sampler2D _MainTex;
            float4 _MainTex_ST;
            float4 _Color;
            fixed _AlphaScale;

            struct appdata
            {
                float4 vertex : POSITION;
                float3 normal:NORMAL;
                float4 texcoord:TEXCOORD0;
            };

            struct v2f
            {
                float4 pos:SV_POSITION;
                float3 worldNormal:TEXCOORD0;
                float3 worldPos:TEXCOORD1;
                float2 uv:TEXCOORD2;

            };



            v2f vert (appdata v)
            {
                
                v2f o;
                o.pos=UnityObjectToClipPos(v.vertex);
                o.worldNormal=UnityObjectToWorldNormal(v.normal);
                o.worldPos=mul(unity_ObjectToWorld,v.vertex).xyz;
                o.uv=TRANSFORM_TEX(v.texcoord,_MainTex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed3 worldNormal=normalize(i.worldNormal);
                fixed3 worldLightDir=normalize(UnityWorldSpaceLightDir(i.worldPos));

                fixed4 texColor=tex2D(_MainTex,i.uv);

                //----------------------[透明度混合 (alpha blend)]-----------------------
                /*
                *   啥也不用写,只是把透明测试的代码移除了就好
                *   我们前面已经用Blend命令打开了混合后,后面设置该片元的透明通道才有意义
                */


                
                fixed3 albedo=texColor.rgb*_Color.rgb;
                fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz*albedo;
                fixed3 diffuse=_LightColor0.rgb*albedo*max(0,dot(worldNormal,worldLightDir));
                //这里的alpha通道不再是1了,我们自己规定了相应的东西
                return fixed4(ambient+diffuse,texColor.a*_AlphaScale);


            }
            ENDCG
        }
    }
    FallBack"Transparent/Cutout/VertexLit"
}
